package toubiao.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.batik.css.engine.value.svg.OpacityManager;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import toubiao.dao.business.DepartmentDaoI;
import toubiao.dao.business.SelectDaoI;
import toubiao.dao.business.SelectOptionDaoI;
import toubiao.domain.en.ProjectType;
import toubiao.domain.po.business.PoDepartment;
import toubiao.domain.po.business.PoSelect;
import toubiao.domain.po.business.PoSelectOption;
import toubiao.domain.vo.assist.Json;
import toubiao.domain.vo.easyUi.EzSelectOption;
import toubiao.utils.ProConstant;
import toubiao.utils.ProUtilConfig;
import toubiao.utils.ProUtilLogger;
import utils.UtilBean;

@Service
public class SelectServiceImpl {

	@Autowired
	private SelectDaoI selectDao;
	
	@Autowired
	private SelectOptionDaoI selectOptionDao;
	
	@Autowired
	private DepartmentDaoI departmentDao;
	
	private static Logger logger=ProUtilLogger.rootLogger;
	
	
	public Json getEzSelectOptionOfProjectDetail() {
		Json json=new Json();
		String className="project";
		Map<String,List<EzSelectOption>> projectMap=new HashMap<>();
		Map<String,List<EzSelectOption>> departmentMap=getEzSelectOptionMapOfDepartment();
		Map<String,List<EzSelectOption>> fixedMap=getEzSelectOptionByClassNameFromDatabase(className);
		
		logger.info(departmentMap.size()+"");
		logger.info(fixedMap.size()+"");
		projectMap.putAll(getSelectOptionOfProjectType());
		
		if(departmentMap.size()>0 && fixedMap.size()>0){
			projectMap.putAll(departmentMap);
			projectMap.putAll(fixedMap);
			json.setSuccess(true);
			json.setData(projectMap);
		}else{
			json.setMessage("加载 selectOption错误.");
		}
		
		return json;
		
	}

	public Map<String, List<EzSelectOption>> getEzSelectOptionMapOfDepartment(){
		Map<String,List<EzSelectOption>> map=new HashMap<>();
		String keyString="department";
		
		List<PoDepartment> list=departmentDao.getAll(PoDepartment.class);
		List<EzSelectOption> ezSelectOptionList=new ArrayList<>();
		for(PoDepartment department:list){
			EzSelectOption option=new EzSelectOption(department.getId()+"", department.getName());
			ezSelectOptionList.add(option);
		}
		map.put(keyString, ezSelectOptionList);
		return map;
	}
	
	
	
	    /**
	    * @Title: getFixedEzSelectOptionByClassName
	    * @Description: 获得 和 实体对象没有关联的EzSelectOptionMap 对象
	    * @param @param className
	    * @param @return    
	    * @return Map<String,List<EzSelectOption>>    
	    * @throws
	    */
	public Map<String, List<EzSelectOption>> getEzSelectOptionByClassNameFromDatabase(String className) {
		Map<String,List<EzSelectOption>> map=new HashMap<>();
		if(className==null){
			return map;
		}
		
		String hql="select  p from PoSelect p  where p.className=:className";
		Map<String,Object> paraMap=new HashMap<>();
		paraMap.put("className", className);
		List<PoSelect> selectList=selectDao.find(hql,paraMap);
		
		logger.info(selectList.size()+"");
		
		for(PoSelect poSelect:selectList){
			String fieldName=poSelect.getClassFieldName();
			List<EzSelectOption> ezSelectOptionList=new ArrayList<>();
			if(fieldName!=null && ! fieldName.trim().equals("")){
				Set<PoSelectOption> optionSet=poSelect.getSelectOptionSet();
				for(PoSelectOption option:optionSet){
					EzSelectOption ezSelectOption=new EzSelectOption(option.getValue(), option.getText());
					ezSelectOptionList.add(ezSelectOption);
				}
				
				map.put(fieldName, ezSelectOptionList);
			}
		}
		return map;
	}
	
	public  Map<String, List<EzSelectOption>> getSelectOptionOfProjectType(){
		Map<String,List<EzSelectOption>> map=new HashMap<>();
		List<EzSelectOption> list=new ArrayList<>();
		ProjectType[] types=ProjectType.values();
		
		for(ProjectType type:types){
			EzSelectOption option=new EzSelectOption();
			option.setText(type.getName());
			option.setValue(type.name());
			list.add(option);
		}
		map.put("projectType", list);
		//Enum enumBean=clazz
		return map;
	}
	
	/*public Json getSelectByClassName(String className) {
		// TODO Auto-generated method stub
		logger.info(className);
		
		Json json=new Json();
		if(className==null){
			json.setMessage("className is null");
			return json;
		}
		
		Map<String, Set<PoSelectOption>> classSelectMap=new HashMap<>();
		
		String hql="select  p from PoSelect p  where p.className=:className";
		Map<String,Object> paraMap=new HashMap<>();
		paraMap.put("className", className);
		List<PoSelect> selectList=selectDao.find(hql,paraMap);
		for(PoSelect poSelect:selectList){
			String fieldName=poSelect.getClassFieldName();
			if(fieldName!=null && ! fieldName.trim().equals("")){
				Set<PoSelectOption> optionSet=poSelect.getSelectOptionSet();
				//防止 后面的 json化的时候 ，出现 循环 转换
				Set<PoSelectOption> optionSetCopy=null;
				try {
					optionSetCopy=UtilBean.getCopyInludeProperties(PoSelectOption.class, optionSet,
							"value","name");
				} catch (InstantiationException | IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					json.setMessage("selectOption 复制失败.");
				}
				classSelectMap.put(fieldName, optionSetCopy);
			}
		}
		
		json.setSuccess(true);
		json.setData(classSelectMap);
		return json;
	}*/

}
